package com.aura.springbook.controller;

import com.aura.springbook.common.R;
import com.aura.springbook.common.util.ResponseUtil;
import com.aura.springbook.model.BookCategory;
import com.aura.springbook.service.BookCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@RestController
@RequestMapping("/api/categories")
@Tag(name = "图书分类接口", description = "图书分类相关操作接口")
public class BookCategoryController {
    
    private static final Logger logger = LoggerFactory.getLogger(BookCategoryController.class);
    
    @Autowired
    private BookCategoryService bookCategoryService;
    
    /**
     * 查询所有分类
     */
    @GetMapping
    @Operation(summary = "查询所有分类", description = "查询所有图书分类")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "查询成功")
    })
    public R<List<BookCategory>> getAllCategories() {
        logger.debug("收到查询所有分类请求");
        
        List<BookCategory> categories = bookCategoryService.getAllCategories();
        logger.debug("查询所有分类成功, 总数: {}", categories.size());
        return ResponseUtil.success(categories);
    }
    
    /**
     * 查询单个分类
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询分类详情", description = "根据分类ID查询分类详细信息")
    @Parameter(name = "id", description = "分类ID", required = true)
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "查询成功"),
        @ApiResponse(responseCode = "404", description = "分类不存在")
    })
    public R<BookCategory> getCategoryById(@PathVariable Long id) {
        logger.debug("收到查询分类详情请求, 分类ID: {}", id);
        
        BookCategory category = bookCategoryService.getCategoryById(id);
        if (category != null) {
            logger.debug("查询分类详情成功, 分类ID: {}, 分类名称: {}", id, category.getName());
            return ResponseUtil.success(category);
        } else {
            logger.warn("查询分类详情失败, 分类ID: {} 不存在", id);
            return ResponseUtil.notFound();
        }
    }
}